import * as types from './constant'
import api from '../../../config'

/**
 * 全局布局状态相关
 */
export default {
  state: {
    // 路由title
    title: '',
    // 动画效果
    direction: 'forward',
    // 全局loading
    loading: false,
    // 页面title
    pageTitle: '',
    // 页面类型
    pageType: ''
  },
  actions: {
    title ({ commit }, title) {
      commit(types.APP_TITLE, title)
    },
    direction ({ commit }, direction) {
      commit(types.APP_DIRECTION, direction)
    },
    loading ({ commit }, loading = true) {
      commit(types.APP_LOADING, loading)
    },
    setPageTitle ({ commit }, pageTitle) {
      commit(types.PAGE_TITLE, pageTitle)
    },
    setPageType ({ commit }, pageType) {
      commit(types.PAGE_TYPE, pageType)
    },
    setPageUrl ({ commit }, pageUrl) {
      var realURL = api + '/' + this.state.pageTitle
      commit(types.PAGE_URL, realURL)
    },
    setPageImg ({ commit }, pageImg) {
      commit(types.PAGE_IMAGE, pageImg)
    },
    setPageWidth ({ commit }, pageWidth) {
      commit(types.PAGE_WIDTH, pageWidth)
    },
    setPageNum ({ commit }, pageNum) {
      commit(types.PAGE_NUM)
    }
  },
  mutations: {
    [types.APP_TITLE] (state, payload) {
      document.title = payload
      state.title = payload
    },
    [types.APP_DIRECTION] (state, payload) {
      state.direction = payload
    },
    [types.APP_LOADING] (state, payload) {
      state.loading = payload
    },
    [types.PAGE_TITLE] (state, payload) {
      state.pageTitle = payload
    },
    [types.PAGE_TYPE] (state, payload) {
      state.pageType = payload
    },
    [types.PAGE_WIDTH] (state, payload) {
      state.pageWidth = payload
    },
    [types.PAGE_URL] (state, payload) {
      state.pageUrl = payload
    },
    [types.PAGE_IMAGE] (state, payload) {
      state.pageImg = payload
    },
    [types.PAGE_WIDTH] (state, payload) {
      state.pageWidth = payload
    },
    [types.PAGE_NUM] (state) {
      state.pageNum++
    }
  },
  getters: {
    loading (state) {
      return state.loading
    },
    direction (state) {
      return state.direction
    },
    pageTitle (state) {
      return state.pageTitle
    },
    pageUrl (state) {
      return state.pageUrl
    },
    pageImg (state) {
      return state.pageImg
    },
    pageWidth (state) {
      return state.pageWidth
    },
    pageNum (state) {
      return state.pageNum
    }
  }
}
